//
//  YCMonthView.m
//  lianganyuchu
//
//  Created by iMac on 17/2/24.
//  Copyright © 2017年 杭州桑缇电子商务有限公司. All rights reserved.
//

#import "YCMonthView.h"

@interface YCMonthView()
{
    NSMutableArray *m_arrayBtns;
    NSInteger _selectIndex;
}
@end
@implementation YCMonthView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self addSubviewsWith:frame];
    }
    return self;
}

-(void)addSubviewsWith:(CGRect)frame
{
    [self setBackgroundColor:BACK_COLOR];
    
    CGFloat width=frame.size.width;
    UIView *header=[[UIView alloc]initWithFrame:CGRectMake(0, 0, width, 40)];
    [self addSubview:header];
    UILabel *label=[[UILabel alloc]init];
    [label setTextColor:TEXT_LIGHT];
    [label setFont:[UIFont LightFontOfSize:14]];
    [label setText:@"选择月份"];
    [header addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(header).offset(kDistance);
        make.right.equalTo(header).offset(-kDistance);
        make.top.equalTo(header).offset(0);
        make.bottom.equalTo(header).offset(0);
    }];
}
-(void)addSubViewsWith:(NSInteger)index
{
    _selectIndex=index;
    UIView *backView=[[UIView alloc]init];
    [backView setBackgroundColor:WHITE_COLOR];
    [self addSubview:backView];
    [backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(0);
        make.right.equalTo(self).offset(0);
        make.top.equalTo(self).offset(40);
        make.height.mas_equalTo(150);
    }];
    
    CGFloat btnWidth=(W(self)-2*kDistance)/4;
    CGFloat btnHeight=50;
    [backView.layer addSublayer:getLine(0, W(self), 50, 50, LINE_COLOR)];
    [backView.layer addSublayer:getLine(0, W(self), 100, 100, LINE_COLOR)];
    for (int i=0; i<3; i++) {
        [backView.layer addSublayer:getLine(kDistance+btnWidth*(i+1), kDistance+btnWidth*(i+1), 10, 140, LINE_COLOR)];
    }
    
    m_arrayBtns=[NSMutableArray array];
    NSArray *arrays=@[@"1月",@"2月",@"3月",@"4月",@"5月",@"6月",@"7月",@"8月",@"9月",@"10月",@"11月",@"12月"];
    for (int i=0; i<arrays.count; i++) {
        int row=i/4;
        int colum=i%4;
        UIButton *button=[[UIButton alloc]init];
        [button setFrame:CGRectMake(colum*btnWidth+kDistance, row*btnHeight, btnWidth, btnHeight)];
        [button setTitleColor:TEXT_DEEP forState:UIControlStateNormal];
        [button setTitleColor:MAIN_COLOR forState:UIControlStateSelected];
        [button setTitle:arrays[i] forState:UIControlStateNormal];
        [button.titleLabel setFont:[UIFont LightFontOfSize:15]];
        [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
        button.tag=i;
        if (button.tag==_selectIndex) {
            [button setSelected:YES];
            button.layer.borderColor=MAIN_COLOR.CGColor;
            button.layer.borderWidth=1.0f;
            button.layer.cornerRadius=2.0f;
        }
        [backView addSubview:button];
        [m_arrayBtns addObject:button];
    }
}

-(void)buttonClicked:(UIButton *)button
{
    _selectIndex=button.tag;
    for (int i=0; i<m_arrayBtns.count; i++) {
        UIButton *buttonM=m_arrayBtns[i];
        if (buttonM.tag==_selectIndex) {
            buttonM.layer.borderColor=MAIN_COLOR.CGColor;
            buttonM.layer.borderWidth=1.0f;
            buttonM.layer.cornerRadius=2.0f;
            [buttonM setSelected:YES];
            self.monthBlock(buttonM.titleLabel.text);
        }else{
            buttonM.layer.borderWidth=CGFLOAT_MIN;
            [buttonM setSelected:NO];
        }
    }
}


@end






